package com.prim.update.impl;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.format.Formatter;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

import com.prim.update.listener.DefaultPromptClickListener;
import com.prim.update.model.UpdateInfo;
import com.prim.update.interfaces.IUpdateExplorer;
import com.prim.update.interfaces.IUpdatePrompter;

import java.lang.ref.WeakReference;

/**
 * ================================================
 * 作    者：linksus
 * 版    本：1.0
 * 创建日期：3/5 0005
 * 描    述：具体实现者 默认的UI提示
 * 重要的事情说三遍
 * 具体的UI 显示规则等需要自己实现
 * 具体的UI 显示规则等需要自己实现
 * 具体的UI 显示规则等需要自己实现
 * 修订历史：
 * ================================================
 */
public class DefultUpdatePrompterUIHandler implements IUpdatePrompter {
    private WeakReference<Context> weakReference;

    public DefultUpdatePrompterUIHandler(Context context) {
        weakReference = new WeakReference<>(context);
    }

    @Override
    public void prompt(IUpdateExplorer explorer, UpdateExplorer.PrompterType type) {
        if (weakReference.get() instanceof Activity && ((Activity) weakReference.get()).isFinishing()) {
            return;
        }
        UpdateInfo info = explorer.getInfo();
        showUIHandler(info, explorer, type);
    }

    private void showUIHandler(UpdateInfo info, IUpdateExplorer explorer, UpdateExplorer.PrompterType type) {
        String size = Formatter.formatShortFileSize(weakReference.get(), 1000);
        String content = String.format("最新版本：%1$s\n新版本大小：%2$s\n\n更新内容\n%3$s", info.getVersion_name(), size, info.getMessage());
        final AlertDialog dialog = new AlertDialog.Builder(weakReference.get()).create();
        dialog.setTitle("应用更新");
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        float density = weakReference.get().getResources().getDisplayMetrics().density;
        TextView tv = new TextView(weakReference.get());
        tv.setMovementMethod(new ScrollingMovementMethod());
        tv.setVerticalScrollBarEnabled(true);
        tv.setTextSize(14);
        tv.setMaxHeight((int) (250 * density));
        dialog.setView(tv, (int) (25 * density), (int) (15 * density), (int) (25 * density), 0);
        DialogInterface.OnClickListener listener = new DefaultPromptClickListener(explorer, true);
        tv.setText(content);
        if (type == UpdateExplorer.PrompterType.install) {
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "立即安装", listener);
            if (explorer.getBuilder().isIgnorable) {
                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "忽略该版", listener);
            } else {
                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", listener);
            }
        } else if (explorer.getBuilder().isEnforce) {
            tv.setText("您需要更新应用才能继续使用\n\n" + content);
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", listener);
            if (explorer.getBuilder().isIgnorable) {
                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "忽略该版", listener);
            } else {
                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", listener);
            }
        } else if (info.isEnforce()) {//服务器设置强制更新
            tv.setText("您需要更新应用才能继续使用\n\n" + content);
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", listener);
            if (explorer.getBuilder().isIgnorable) {
                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "忽略该版", listener);
            } else {
                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", listener);
            }
        } else {
            if (type == UpdateExplorer.PrompterType.update) {
                dialog.setButton(DialogInterface.BUTTON_POSITIVE, "立即更新", listener);
                if (explorer.getBuilder().isIgnorable) {
                    dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "忽略该版", listener);
                } else {
                    dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "以后再说", listener);
                }
            }
//            if (info.isIgnorable) {//可以不更新该版本
//                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "忽略该版", listener);
//            }
        }
        dialog.show();
    }
}
